<?php

namespace App\Http\Controllers;

use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use Illuminate\Support\Str;
use TencentCloud\Sms\V20190711\SmsClient;
use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use Illuminate\Support\Facades\Log;
use App\Jobs\SyncSendSms;

class LoginController extends Controller
{

    public function getLoginPhone(Request $request)
    {
        return view('login');
    }

    public function postCaptcha(Request $request)
    {
        $phone = $request->input('phone');
        $rules = ['captcha' => 'required|captcha'];
        $validator = validator()->make(request()->all(), $rules);
        if ($validator->fails()) {
            smilify('error', '图形验证码错误');
            return redirect('/login');
        } else {
            try {
                //发送短信
                $sms_code = mt_rand(1000, 9999);
                $content = '【欢生活】您的手机验证码为'.$sms_code.', 十分钟有效。如非本人操作请忽视';
                SyncSendSms::dispatch('phone', $phone, $content);

                $request->session()->put('SmsCode_' . $phone, $sms_code);
                $request->session()->put('phone' , $phone);

                return view('login_captcha', ['phone' => $phone]);

            } catch (TencentCloudSDKException $e) {
                smilify('error', '短信发送错误:'.$e->getMessage());
                return redirect('/login');
            } catch (Exception $e) {
                smilify('error', '短信发送错误:'.$e->getMessage());
                return redirect('/login');
            }
        }
    }

    public function postLoginPhone(Request $request)
    {

        $phone = $request->input('phone');
        $captcha = $request->input('captcha');
        $smsCode = $request->session()->get('SmsCode_' . $phone);

        Log::info($phone ."\t". $captcha ."\t". $smsCode);
        if ($smsCode != $captcha) {
            smilify('error', '短信验证码错误');
            return redirect('/login');
        }


        $user = User::firstOrCreate(
            ['phone' => $phone],
            [
                'phone' => $phone,
                'name' => '手机用户' . Str::substr($phone, -4, 4),
                'email' => $phone . "@139.cn",
                'password' => bcrypt(Str::random(8))
            ]
        );
        Auth::login($user, true);
        return redirect('/');
    }


    /**
     * 处理认证尝试
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return Response
     */
    public function authenticate(Request $request)
    {
        $credentials = $request->only('email', 'password');

        Auth::loginUsingId(1);
        //if (Auth::attempt($credentials)) {
        return redirect()->intended('dashboard');
        //}
    }

    public function isPhone($phone) 
    {
        if (strlen($phone) <= 11) {
            return false;
        } else {
            return true;
        }
    }

    public function fixPhone($phone, $code = "+86")
    {
        if (strpos($phone, $code) !== 0) {
            return $code . $phone;
        } else {
            return $phone;
        }
    }
}
